﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CommandDeleteMarks : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			FormDeleteMarks formDeleteMarks = new FormDeleteMarks();
			if (CommandDeleteMarks.hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				CommandDeleteMarks.hWndRevit = new YJKWindowHandle(mainWindowHandle);
			}
			formDeleteMarks.Show(CommandDeleteMarks.hWndRevit);
			Transaction transaction = new Transaction(base.Doc, "删除标注");
			Result result;
			try
			{
				for (;;)
				{
					ICollection<Element> collection = base.Sel.PickElementsByRectangle("请框选删除范围");
					if (collection != null)
					{
						if (collection.Count >= 1)
						{
							ICollection<ElementId> collection2 = (from ele in collection
							select ele.Id).ToList<ElementId>();
							transaction.Start();
							DeleteMarksHelper.DeleteFilterGroup(formDeleteMarks.ListTypes, base.Doc, collection2);
							DeleteMarksHelper.DeleteFilterTags(formDeleteMarks.ListTypes, base.Doc, collection2);
							transaction.Commit();
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
                if (transaction.GetStatus() == (TransactionStatus)1)
				{
					transaction.Commit();
				}
				formDeleteMarks.Close();
				formDeleteMarks.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
                if (transaction.GetStatus() != (TransactionStatus)3)
				{
					transaction.RollBack();
				}
				formDeleteMarks.Close();
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
